package ui;

import config.GameConfig;
import entity.GameAct;

import java.awt.*;

public class LayerGame extends Layer {


    /**
     * 左位移偏移量
     */
    private static final int SIZE_ROL= GameConfig.getFrameConfig().getSizeRol();

    //TODO 硬编码
    private static final int LEFT_SIZE = 0;

    private static final int RIGHT_SIDE = GameConfig.getSystemConfig().getMaxX();

    private static final int LOSE_IDX = GameConfig.getFrameConfig().getLoseIdx();

    public LayerGame(int x, int y, int w, int h) {
        super(x, y, w, h);
    }

    @Override
    public void paint(Graphics g){
        this.createWindow(g);
        // 获得方块数组集合
        GameAct act = this.dto.getGameAct();
        if(act != null){
            Point[] points = act.getActPoints();
            // 绘制阴影
            this.drawShadow(points,g);
            // 绘制活动方块
            this.drawMainAct(points, g);
        }
        // 绘制游戏地图
        this.drawMap(g);
        //暂停
        if(this.dto.isPause()){
            this.drawImageAtcenter(Img.PAUSE,g);
        }
    }
    /**
     * 绘制活动方块
     */
    private void drawMainAct(Point[] points,Graphics g) {
        //获得方块类型编号(0-6)
        int typeCode=this.dto.getGameAct().getTypeCode();
        //绘制方块
        for (int i=0;i<points.length;i++){
            this.drowActByPoint(points[i].x,points[i].y,typeCode + 1,g);
        }
    }
    /**
     * 绘制游戏地图
     */
    private void drawMap(Graphics g) {
        //绘制地图
        boolean[][] map=this.dto.getGameMap();
        //计算当前堆积颜色
        int Lv = this.dto.getNowlevel();
        int imgIdx = Lv == 0 ? 0 : (Lv - 1) % 7 +1;
        for(int x=0;x<map.length;x++) {
            for(int y=0;y<map[x].length;y++) {
                if(map[x][y]){
                    this.drowActByPoint(x,y,imgIdx,g);
                }
            }
        }
    }


    /**
     * 绘制阴影
     */
    private void drawShadow(Point[] points,Graphics g) {

        if(!this.dto.isShowShadow()) {
            return;
        }
        int leftX = RIGHT_SIDE;
        int rightX = LEFT_SIZE;
        for (Point p : points) {
            leftX = p.x < leftX ? p.x : leftX;
            rightX = p.x > rightX ? p.x : rightX;
        }
        g.drawImage(Img.SHODOW,this.x + BORDER + (leftX << SIZE_ROL) ,
                this.y + BORDER,
                (rightX - leftX + 1) << SIZE_ROL,
                this.h - (BORDER << 1),
                null);
    }
    /**
     * 绘制正方形快
     */
    private void drowActByPoint(int x,int y,int imgIdx,Graphics g){
        imgIdx = this.dto.isStart() ? imgIdx :LOSE_IDX;
        g.drawImage(Img.ACT,
                this.x+(x<<SIZE_ROL) + BORDER,
                this.y+(y<<SIZE_ROL) + BORDER,
                this.x+(x+1<<SIZE_ROL) + BORDER,
                this.y+(y+1<<SIZE_ROL) + BORDER,
                imgIdx<<SIZE_ROL,0,(imgIdx+1) << SIZE_ROL,1 << SIZE_ROL,null);
    }
}
